import random

# 定义卡牌类
class Card:
    def __init__(self, name, attack, health):
        self.name = name
        self.attack = attack
        self.health = health

    def __str__(self):
        return f"{self.name} (攻击: {self.attack}, 生命值: {self.health})"

# 定义玩家类
class Player:
    def __init__(self, name):
        self.name = name
        self.hand = []
        self.deck = [
            Card("勇士", 3, 5),
            Card("法师", 4, 3),
            Card("刺客", 5, 2),
            Card("射手", 4, 4),
            Card("牧师", 2, 6)
        ]
        self.shuffle_deck()
        self.draw_hand()

    def shuffle_deck(self):
        random.shuffle(self.deck)

    def draw_hand(self):
        for _ in range(3):
            if self.deck:
                self.hand.append(self.deck.pop())

    def show_hand(self):
        print(f"{self.name} 的手牌:")
        for i, card in enumerate(self.hand):
            print(f"{i}. {card}")

    def play_card(self, index):
        if 0 <= index < len(self.hand):
            return self.hand.pop(index)
        return None

# 战斗函数
def battle(player1, player2):
    turn = 0
    while player1.hand and player2.hand:
        print(f"\n第 {turn + 1} 回合:")
        print(f"{player1.name} 的回合:")
        player1.show_hand()
        while True:
            try:
                choice = int(input("请选择要出的卡牌索引: "))
                card1 = player1.play_card(choice)
                if card1:
                    break
                else:
                    print("无效的索引，请重新选择。")
            except ValueError:
                print("输入无效，请输入一个数字。")

        print(f"{player2.name} 的回合:")
        player2.show_hand()
        while True:
            try:
                choice = int(input("请选择要出的卡牌索引: "))
                card2 = player2.play_card(choice)
                if card2:
                    break
                else:
                    print("无效的索引，请重新选择。")
            except ValueError:
                print("输入无效，请输入一个数字。")

        # 卡牌战斗
        print(f"{player1.name} 出了 {card1.name}，{player2.name} 出了 {card2.name}")
        card1.health -= card2.attack
        card2.health -= card1.attack
        if card1.health > 0 and card2.health <= 0:
            print(f"{player1.name} 的 {card1.name} 击败了 {player2.name} 的 {card2.name}")
        elif card2.health > 0 and card1.health <= 0:
            print(f"{player2.name} 的 {card2.name} 击败了 {player1.name} 的 {card1.name}")
        elif card1.health <= 0 and card2.health <= 0:
            print("双方卡牌同归于尽")
        else:
            print("双方卡牌都存活")

        turn += 1

    if not player1.hand and not player2.hand:
        print("平局！双方都没有手牌了。")
    elif not player1.hand:
        print(f"{player2.name} 获胜！")
    else:
        print(f"{player1.name} 获胜！")

# 主函数
def main():
    player1 = Player("玩家 1")
    player2 = Player("玩家 2")
    battle(player1, player2)

if __name__ == "__main__":
    main()